package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskCompanyMapper;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskContactMapper;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskCompany;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskContact;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskContactService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Jay.Yang
 */

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskContactServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskContactMapper, TaskAutotaskContact>
        implements TaskAutotaskContactService {

    private final TaskAutotaskCompanyMapper autotaskCompanyMapper;

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task contact receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskContact data = TaskAutotaskContact.builder()
                .contactId(super.getLongValue(entity.get("id")))
                .companyId(ObjectUtils.isEmpty(entity.get("companyID")) ? null : entity.get("companyID").asLong())
                .phone(ObjectUtils.isEmpty(entity.get("phone")) ? null : entity.get("phone").asText())
                .city(ObjectUtils.isEmpty(entity.get("city")) ? null : entity.get("city").asText())
                .state(ObjectUtils.isEmpty(entity.get("state")) ? null : entity.get("state").asText())
                .firstName(ObjectUtils.isEmpty(entity.get("firstName")) ? null : entity.get("firstName").asText())
                .lastName(ObjectUtils.isEmpty(entity.get("lastName")) ? null : entity.get("lastName").asText())
                .emailAddress(ObjectUtils.isEmpty(entity.get("emailAddress")) ? null : entity.get("emailAddress").asText())
                .isActive(ObjectUtils.isEmpty(entity.get("isActive")) ? null : entity.get("isActive").asBoolean())
                .createDate(ObjectUtils.isEmpty(entity.get("createDate")) || "null".equalsIgnoreCase(entity.get("createDate").asText()) ? null: LocalDateTime.parse(entity.get("createDate").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .lastActivityDate(ObjectUtils.isEmpty(entity.get("lastActivityDate")) || "null".equalsIgnoreCase(entity.get("lastActivityDate").asText()) ? null: LocalDateTime.parse(entity.get("lastActivityDate").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        if (!ObjectUtils.isEmpty(data.getCompanyId())){
            TaskAutotaskCompany company = this.autotaskCompanyMapper.selectOne(Wrappers.<TaskAutotaskCompany>lambdaQuery()
                    .eq(TaskAutotaskCompany::getTenantCode, data.getTenantCode())
                    .eq(TaskAutotaskCompany::getCompanyId, data.getCompanyId())
                    .last("limit 1"));
            data.setCompanyName(ObjectUtils.isEmpty(company) ? null : company.getCompanyName());
        }

        TaskAutotaskContact dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskContact>lambdaQuery()
                .eq(TaskAutotaskContact::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskContact::getContactId, data.getContactId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
